<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" >
        <el-input :disabled="true" v-model="dataForm.title" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="上架状态" prop="state">
        <el-select v-model="dataForm.state" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="封面:">
        <img width="100%" :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + dataForm.headPhoto" alt="">
      </el-form-item>
      <el-form-item label="身份证正面:">
        <img width="100%" :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + dataForm.identityZ" alt="">
      </el-form-item>
      <el-form-item label="身份证反面:">
        <img width="100%" :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + dataForm.identityF" alt="">
      </el-form-item>
      <el-form-item label="营业执照:">
        <img width="100%" :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + dataForm.businessLicense" alt="">
      </el-form-item>
      <el-form-item label="卫生许可证:">
        <img width="100%" :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + dataForm.hygienicLicense" alt="">
      </el-form-item>
      <el-form-item label="消防证:">
        <img width="100%" :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + dataForm.firePermit" alt="">
      </el-form-item>
      <el-form-item label="门头照片:">
        <img width="100%" :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + dataForm.headPhoto" alt="">
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
         <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="抽佣比例" prop="proportion">
        <el-input v-model="dataForm.proportion" placeholder="抽佣比例"></el-input>
      </el-form-item>
      <el-form-item label="商户余额">
        <el-input :disabled="true"  v-model="dataForm.wallet " placeholder="商户余额"></el-input>
      </el-form-item>
      <el-form-item label="是否推荐" prop="ifTj">
        <el-select v-model="dataForm.ifTj" @change='aaaaa' placeholder="请选择">
          <el-option
            v-for="item in ifTjList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: '',
      visible: false,
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      dataForm: {
        id: "",
        title: "",
        zj: "",
        dj: "",
        images: "",
        sl: "",
        headPhoto: "",
        status: "",
      },
      ifTjList: [
        {
          value: 0,
          label: "否",
        },
        {
          value: 1,
          label: "是",
        },
      ],
      options: [
        {
          value: 0,
          label: "上架",
        },
        {
          value: 1,
          label: "下架",
        },
      ],
      statusList: [
        {
          value: 1,
          label: "已通过",
        },
        {
          value: 2,
          label: "未通过",
        },
      ],
      dataRule: {
        state: [{ required: true, message: "上架状态不能为空", trigger: "blur" }],
        status: [{ required: true, message: "审核状态不能为空", trigger: "blur" }],
        proportion: [{ required: true, message: "抽佣比例不能为空", trigger: "blur" }],
        ifTj: [{ required: true, message: "是否推荐不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    aaaaa(e) {
      console.log(e)
      this.dataForm.ifTj = e
    },
    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 5;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 5MB!');
        }
        return isJPG && isLt2M;
      },
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    init(id) {
      this.dataForm.id = id || "";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `sys/merchant/info?id=${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm= data.merchant;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`sys/merchant/update`),
              method: "post",
              params: this.$http.adornParams({
                id: this.dataForm.id,
                title: this.dataForm.title,
                zj: this.dataForm.zj,
                dj: this.dataForm.dj,
                images: this.dataForm.images,
                sl: this.dataForm.sl,
                headPhoto: this.dataForm.headPhoto,
                status: this.dataForm.status,
                ifTj: this.dataForm.ifTj
              }),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          } else {
            this.$http({
              url: this.$http.adornUrl(`sys/creativegoods/save`),
              method: "post",
              params: this.$http.adornParams({
                title: this.dataForm.title,
                zj: this.dataForm.zj,
                dj: this.dataForm.dj,
                images: this.dataForm.images,
                sl: this.dataForm.sl,
                headPhoto: this.dataForm.headPhoto,
                status: this.dataForm.status,
              }),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        }
      });
    },
  },
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>